﻿#include "uimodbusmapinfo.h"
UiModBusMapInfo::UiModBusMapInfo(QWidget *parent)
    : QWidget{parent}
{
    m_yxview = new UiModbusBase(ESigType::EYx,this);
    m_ykview = new UiModbusBase(ESigType::EYk,this);
    m_ycview = new UiModbusBase(ESigType::EYc,this);
    m_parameterview = new UiModbusBase(ESigType::EParameter, this);

    m_btnadd = new ElaPushButton("添加映射", this);
    connect(m_btnadd, &ElaPushButton::clicked, this, &UiModBusMapInfo::slotAddMap);

    ElaText* lab_maps = new ElaText("选择映射ID:",this);
    lab_maps->setTextPointSize(11);
    m_cbxmaps = new ElaComboBox(this);
    m_cbxmaps->setMinimumWidth(250);
    QHBoxLayout* hlayout = new QHBoxLayout;
    hlayout->addWidget(lab_maps);
    hlayout->addWidget(m_cbxmaps);
    hlayout->addStretch(1);
    hlayout->addWidget(m_btnadd);

    m_tabwidget = new ElaTabWidget(this);
    m_tabwidget->setTabsClosable(false);
    m_tabwidget->setIsTabTransparent(true);
    m_tabwidget->addTab(m_yxview, "遥信映射");
    m_tabwidget->addTab(m_ycview, "遥测映射");
    m_tabwidget->addTab(m_ykview, "遥控映射");
    m_tabwidget->addTab(m_parameterview, "参数映射");
    m_tabwidget->setTabPosition(ElaTabWidget::East);

    QFrame *line = new QFrame();
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Plain); // 设置阴影为无阴影

    QVBoxLayout* mianlayout = new QVBoxLayout;
    mianlayout->addWidget(line);

    line = new QFrame();
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Plain); // 设置阴影为无阴影

    mianlayout->addLayout(hlayout);
    mianlayout->addWidget(line);
    mianlayout->addWidget(m_tabwidget);

    this->setLayout(mianlayout);

    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setStyleSheet("background-color: rgba(0, 0, 0, 0);");

    connect(this, &UiModBusMapInfo::selectMapChange, m_yxview, &UiModbusBase::slotSelectMapChange);
    connect(this, &UiModBusMapInfo::selectMapChange, m_ycview, &UiModbusBase::slotSelectMapChange);
    connect(this, &UiModBusMapInfo::selectMapChange, m_ykview, &UiModbusBase::slotSelectMapChange);
    connect(this, &UiModBusMapInfo::selectMapChange, m_parameterview, &UiModbusBase::slotSelectMapChange);

    connect(m_cbxmaps, &ElaComboBox::currentIndexChanged, this, &UiModBusMapInfo::slotMapChange);
}

void UiModBusMapInfo::slotSelectModelChange(const QString &name, const QString &user) {
    m_user = user;
    m_modelName = name;

    m_maps.clear();

    bool res = CWebRequestApi::ins().getModelModbusMaps(m_user, m_modelName, m_maps);

    m_cbxmaps->blockSignals(true);
    m_cbxmaps->clear();
    for(int i = 0; i < m_maps.size(); ++i) {
        m_cbxmaps->addItem(m_maps.at(i).mapid);
    }
    m_cbxmaps->blockSignals(true);

    if(!res || !m_maps.size()) {
        slotMapChange(-1);
    } else {
        slotMapChange(0);
    }
}

void UiModBusMapInfo::slotMapChange(int index) {
    if(index < 0) {
        emit selectMapChange(m_user, m_modelName, "");
    } else {
        emit selectMapChange(m_user, m_modelName, m_maps.at(index).mapid);
    }
}

void UiModBusMapInfo::slotAddMap() {
    QVBoxLayout* vlayout = new QVBoxLayout;

    ElaText* lab_name = new ElaText("请输入映射名字:");
    ElaLineEdit* edit_name = new ElaLineEdit;
    QHBoxLayout* hlayout = new QHBoxLayout;
    hlayout->addWidget(lab_name);
    hlayout->addWidget(edit_name);
    vlayout->addLayout(hlayout);

    ElaText* lab_descr = new ElaText("请输入映射描述:");
    ElaLineEdit* edit_descr = new ElaLineEdit;
    hlayout = new QHBoxLayout;
    hlayout->addWidget(lab_descr);
    hlayout->addWidget(edit_descr);
    vlayout->addLayout(hlayout);

    UiCustomDialog* dialog = new UiCustomDialog;
    dialog->resize(600, 150);
    dialog->setConten(vlayout);

    while(dialog->exec() == 0) {
        if(edit_name->text().isEmpty()) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "名字不能为空!",2000);
            continue;
        }

        if(edit_descr->text().isEmpty()) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "描述不能为空!",2000);
            continue;
        }

        CModbusMapInfo info;
        info.mapid = QString("%1.%2.%3").arg(m_user,m_modelName,edit_name->text());
        info.userid = m_user;
        info.modelName = m_modelName;
        info.descr = edit_descr->text();
        info.createtime = QDateTime::currentDateTime();

        if(!CWebRequestApi::ins().addModbusMap(info)) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "服务器请求错误,添加失败,!",2000);
        } else {
            ElaMessageBar::success(ElaMessageBarType::Top, "完成", "添加映射成功",2000);
            m_maps.append(info);
            m_cbxmaps->blockSignals(true);
            m_cbxmaps->addItem(info.mapid);
            m_cbxmaps->setCurrentText(info.mapid);
            m_cbxmaps->blockSignals(false);
            slotMapChange(m_maps.size()-1);
        }
        break;
    }
    dialog->deleteLater();
}
